<template>
  <el-main>
    <div class="margin-bottom-20">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/company' }"
          >企业管理
        </el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/company/list' }"
          >企业列表
        </el-breadcrumb-item>
        <el-breadcrumb-item>分配设备展示位置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="clearfix">
      <el-form class="pull-left" inline>
        <el-form-item>
          <el-input
            placeholder="请输入imei"
            v-model="search_param.imei"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="通信方式" v-model="search_param.deviceSocket">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in device_socket"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="设备类型" v-model="search_param.deviceType">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in device_type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="平台信息" v-model="search_param.platformType">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in platform_type"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button-group>
            <el-button
              @click="handleSearch"
              icon="el-icon-search"
              size="medium"
              type="primary"
              >搜索
            </el-button>
            <el-button @click="handleReset" size="medium" type="primary"
              >重置
            </el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="box-card" shadow="never">
      <h4
        class="margin-bottom-20"
        style="border-left: 3px solid #00a7cd;padding-left: 8px;"
      >
        公司信息：
      </h4>

      <div class="margin-bottom-20" style="margin-left: 20px">
        {{ form.companyName }}
      </div>
      <h4
        class="margin-bottom-20"
        style="border-left: 3px solid #00a7cd;padding-left: 8px;"
      >
        设备列表：
      </h4>
      <el-table :data="table_data" stripe style="width: 100%">
        <el-table-column label="imei" prop="imei" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          label="通信方式"
          prop="deviceSocket_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备类型"
          prop="deviceType_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="平台信息"
          prop="platformType_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="所属公司"
          prop="company_name"
          show-overflow-tooltip
        />
        <el-table-column
          label="展示位置"
          prop="webTypeDesc"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleNoToWeb(scope.row)"
              size="mini"
              style="color: red"
              type="text"
              v-if="scope.row.webType"
              >解除设备展示位置
            </el-button>
            <el-button
              @click="showToWebType(scope.row)"
              size="mini"
              type="text"
              v-else
              >分配设备展示位置
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="margin-top-10 clearfix">
        <el-pagination
          :current-page.sync="current_page"
          :page-size.sync="page_size"
          :page-sizes="[5, 10, 20]"
          :total="total_size"
          @current-change="load_table_data"
          @size-change="load_table_data"
          background
          class="pull-right"
          layout="total, sizes,  prev, pager, next"
        />
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" title="分配展示位置" width="50%">
      <div style="padding: 20px 20px 200px">
        <el-form class="pull-left" inline>
          <el-form-item label="展示位置">
            <el-select placeholder="请选择展示位置" v-model="toWebForm.toWeb">
              <el-option
                :key="item.code"
                :label="item.label"
                :value="item.code"
                v-for="item in toWebType"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="handleToWeb" type="primary">确 定 </el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      device_type: [],
      device_socket: [],
      platform_type: [],
      page_size: 5,
      current_page: 1,
      total_size: 0,
      table_data: [],
      search_param: {
        companyId: null,
        imei: null,
        deviceSocket: null,
        deviceType: null,
        platformType: null
      },
      form: {
        companyId: null,
        companyName: null,
        pushUrl: null,
        deviceType: null
      },
      dialogVisible: false,
      toWebForm: {
        deviceId: null,
        toWeb: null
      },
      toWebType: []
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.search_param.companyId = this.id;
    this.info();
    this.load_table_data();
    this.load_device_type();
    this.load_device_socket();
    this.load_platform_type();
    this.toWeb_type();
  },
  methods: {
    showToWebType(row) {
      this.toWebForm.toWeb = null;
      this.toWebForm.deviceId = row.id;
      this.dialogVisible = true;
    },
    info() {
      this.$ajax.company
        .info(this.id)
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
            this.$router.push("/company/list/");
          } else {
            this.form = result.data;
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    load_table_data() {
      this.$ajax.company_device_web
        .list({
          current_page: this.current_page,
          page_size: this.page_size,
          search_param: this.search_param
        })
        .then(response => {
          if (response.code == 1) {
            this.table_data = response.data.records;
            this.total_size = response.data.total;
          } else {
            this.$message({
              message: response.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          return false;
        });
    },
    handleSearch() {
      this.load_table_data();
    },
    handleReset() {
      this.search_param.imei = null;
      this.search_param.deviceSocket = null;
      this.search_param.deviceType = null;
      this.search_param.platformType = null;
      this.load_table_data();
    },
    handleNoToWeb(row) {
      let loadingInstance = this.$loading({ fullscreen: true });
      this.$ajax.company_device_web
        .unbind({ deviceId: row.id, companyId: this.id })
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          } else {
            this.$message({
              type: "success",
              message: "解除设备展示位置成功!"
            });
            this.load_table_data();
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        })
        .finally(function() {
          loadingInstance.close();
        });
    },
    handleToWeb() {
      let loadingInstance = this.$loading({ fullscreen: true });
      this.$ajax.company_device_web
        .bind({
          companyId: this.id,
          deviceId: this.toWebForm.deviceId,
          toweb: this.toWebForm.toWeb
        })
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          } else {
            this.$message({
              type: "success",
              message: "分配设备展示位置成功!"
            });
            this.load_table_data();
            this.dialogVisible = false;
            this.toWebForm.toWeb = null;
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        })
        .finally(function() {
          loadingInstance.close();
        });
    },
    load_device_type() {
      this.$ajax.support
        .device_type()
        .then(result => {
          if (result.code === 1) {
            this.device_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_device_socket() {
      this.$ajax.support
        .device_socket()
        .then(result => {
          if (result.code === 1) {
            this.device_socket = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_platform_type() {
      this.$ajax.support
        .platform_type()
        .then(result => {
          if (result.code === 1) {
            this.platform_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    toWeb_type() {
      this.$ajax.support
        .toWeb_type()
        .then(result => {
          if (result.code === 1) {
            this.toWebType = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
};
</script>
